﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for Name
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class Name : System.Web.Services.WebService
{

    public Name()
    {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    /// <summary>
    /// Gets the name list.
    /// </summary>
    /// <param name="prefixText">The prefix text.</param>
    /// <param name="count">The count.</param>
    /// <returns></returns>
    [WebMethod]
    public string[] GetNameList(string prefixText, int count)
    {
        List<string> items = new List<string>(count);//泛型 
        SqlConnection myCon = new SqlConnection("Data Source=.;Initial Catalog=cnpctest;Integrated Security=True");//数据库连接 
        myCon.Open();//打开数据库连接 
        SqlCommand myCmd = new SqlCommand("select top " + count + " name_string from taxon where name_string like '" + prefixText + "%'group by name_string order by name_string", myCon);

        SqlDataReader myDR = myCmd.ExecuteReader();
        while (myDR.Read())
        {
            items.Add(myDR["name_string"].ToString());
        }
        myCon.Close();//关闭数据库连接 
        return items.ToArray();

    }   

}

